package com.zivwu.library;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zivwu.library.ui.base.IActivity;
import com.zivwu.library.widget.banner.Banner;
import com.zivwu.library.widget.banner.BannerAdapter;

import net.lucode.hackware.magicindicator.abs.IPagerNavigator;
import net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class TestBannerActivity extends IActivity {

    private Banner banner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_banner);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.tv_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        List<String> mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
        }
        StringAdapter adapter = new StringAdapter(mDatas);
        recyclerView.setAdapter(adapter);


        String[] stringArray = getResources().getStringArray(R.array.imgs);
        List<String> strings = Arrays.asList(stringArray);
        banner = new Banner(this);
        banner.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));

        adapter.addHeaderView(banner);

        banner.setAdapter(new BannerAdapter<String>(strings, banner.getPager()) {
            @NonNull
            @Override
            protected IPagerNavigator instanceNavigator(LinkedList<String> strings) {
                CircleNavigator circleNavigator = getCircleNavigator();
                circleNavigator.setCircleCount(strings.size() > 1 ? strings.size() : 0);
                return circleNavigator;
            }

            @Override
            public void onInflateView(String s, View view) {
                Glide.with(TestBannerActivity.this)
                        .asBitmap()
                        .load(s)
                        .apply(new RequestOptions().centerCrop())
                        .into((ImageView) view);
            }
        });
//        banner.startAutoPlay();

    }

    @Override
    protected void onStart() {
        super.onStart();
        banner.startAutoPlay();


    }


    private CircleNavigator getCircleNavigator() {
        CircleNavigator navigator = new CircleNavigator(this);
        navigator.setRadius(10);
        navigator.setCircleColor(ContextCompat.getColor(this, android.R.color.holo_green_light));
        return navigator;
    }


    private class StringAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public StringAdapter(@Nullable List<String> data) {
            super(R.layout.item_demo, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.id_num, item);
        }
    }
}
